﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class StringToByteArray
    {
        public void STBTest()
        {
            string testStr = "This is a Test String";
            //utf8转换
            Encoding utf8enc=Encoding.UTF8;
            byte[] utf8Arr = utf8enc.GetBytes(testStr);
            Console.WriteLine(utf8Arr.Length);
            //GB2312转换
            Encoding gb2313enc = Encoding.GetEncoding("GB2312");
            byte[] gbArr = gb2313enc.GetBytes(testStr);
            Console.WriteLine(gbArr.Length);

            //转回String
            Console.WriteLine(utf8enc.GetString(utf8Arr));

        }

    }
}
